package com.umcg.monitor;

import org.json.JSONObject;
import org.json.JSONArray;
import android.webkit.WebView;

public class ClassicMonitor {
	private WebView mAppView;
	private String colorofLine;
	private String colorofLine2;
	private String colorofLine3;
	private String colorofLine4;
	public boolean click = false;
	Integer[] dataECG;
	Integer[] dataPlet;
	Integer[] dataCO2;
	Integer[] dataPAW;

	int[] onScreenECG = new int[200]; // waarden die op dit moment op het scherm
										// worden weergegeven.
	int[] onScreenPlet = new int[200];
	int[] onScreenCO2 = new int[200];
	int[] onScreenPAW = new int[200];

	public ClassicMonitor(WebView appView, String lineColor, String lineColor2,
			String lineColor3, String lineColor4, Integer[] ECGdata,
			Integer[] Pletdata, Integer[] CO2data, Integer[] PAWdata) {
		dataECG = ECGdata;
		dataPlet = Pletdata;
		dataCO2 = CO2data;
		dataPAW = PAWdata;
		this.mAppView = appView;
		appView.getSettings().setJavaScriptEnabled(true);
		appView.addJavascriptInterface(this, "testhandler");
		setColorofLine(lineColor, lineColor2, lineColor3, lineColor4);
		appView.loadUrl("file:///android_asset/flot/plot/classic.html");
		appView.setBackgroundColor(0);
	}

	public String getGraphTitle() {
		return "This is my graph, baby!";
	}

	public void plot(Integer[] ECGdata, Integer[] Pletdata, Integer[] CO2data,
			Integer[] PAWdata, int start) {
		JSONArray resECG = new JSONArray();
		JSONArray resPlet = new JSONArray();
		JSONArray resCO2 = new JSONArray();
		JSONArray resPAW = new JSONArray();

		// stap 1. plot Data
		if (start == -1) { // initial plot

			resECG = ECGData(dataECG, 200, 0);
			resPlet = PletData(dataPlet, 200, 0);
			resCO2 = CO2Data(dataCO2, 200, 0);
			resPAW = PAWData(dataPAW, 200, 0);

			mAppView.loadUrl("javascript:setAllInOne( " + resECG.toString() + ","
					+ resPlet.toString() + "," + resCO2.toString() + ","
					+ resPAW.toString() + " )");
		} else {

			resECG = updateECG(ECGdata, 200, start);
			resPlet = updatePlet(Pletdata, 200, start);
			resCO2 = updateCO2(CO2data, 200, start);
			resPAW = updatePAW(PAWdata, 200, start);

			mAppView.loadUrl("javascript:updateAllInOne( " + resECG.toString() + ","
					+ resPlet.toString() + "," + resCO2.toString() + ","
					+ resPAW.toString() + " )");

		}

	}

	public void plotData() { // calls a plot function in javascript and draws
								// the data

		// JSONArray data = new JSONArray();
		JSONObject options1 = new JSONObject();
		JSONObject options2 = new JSONObject();
		JSONObject options3 = new JSONObject();
		JSONObject options4 = new JSONObject();

		try {
			// data = multiLinesData(1);
			options1 = getLineOptionsJSON(this.colorofLine, -200, 500);
			options2 = getLineOptionsJSON(this.colorofLine2, 0, 5000);
			options3 = getLineOptionsJSON(this.colorofLine3, 0, 1500);
			options4 = getLineOptionsJSON(this.colorofLine4, 0, 500);
		} catch (Exception ex) {
			// do something
		}
		mAppView.loadUrl("javascript:setOptions(" + options1.toString() + ", "
				+ options2.toString() + ", " + options3.toString() + ", "
				+ options4.toString() + ")");
		// mAppView.loadUrl("javascript:setData(" + ecgData + ", " +
		// options1.toString() + ")");
		// mAppView.loadUrl("javascript:random()");

	}

	public void clicked() {
		click = true;
	}

	public JSONArray PAWData(Integer[] lines, int length, int start) { // Only
																		// called
																		// first
																		// time
		JSONArray res = new JSONArray();

		for (int i = 0; i < length; ++i) {
			res.put(createDataPoint(i, lines[i]));
			onScreenPAW[i] = lines[i];

		}

		return res;
	}

	public JSONArray updatePAW(Integer[] lines, int length, int start) {// lines
																		// =
																		// array
																		// met
																		// waarden,
																		// int
																		// length
																		// =
																		// aantal
																		// punten
																		// van
																		// de
																		// grafiek,
																		// start
																		// is
																		// huidige
																		// index
		JSONArray res = new JSONArray();
		// start = start*2;
		if (start < (lines.length - length)) {
			for (int i = 0; i <= start; i++) {
				if (i < onScreenPAW.length) {
					onScreenPAW[i] = lines[length + i];
				} else {
					onScreenPAW[i % onScreenPAW.length] = lines[length + i];
				}
			}
		} else {
			for (int j = 0; j < start; j++) {
				if (j < (lines.length - length) && j < onScreenPAW.length) {
					onScreenPAW[j] = lines[length + j];
				} else {
					onScreenPAW[j % onScreenPAW.length] = lines[length + j];
				}

			}
		}

		for (int i = 0; i < length; i++) {
			res.put(createDataPoint(i, onScreenPAW[i]));
			if (start < i) {
				if (i == start) {
					res.put(createBreakPoint());
				}
			} else {
				if (i == start % length) {
					res.put(createBreakPoint());
				}
			}
		}

		return res;
	}

	public JSONArray PletData(Integer[] lines, int length, int start) { // Only
																		// called
																		// first
																		// time
		JSONArray res = new JSONArray();

		for (int i = 0; i < length; ++i) {
			res.put(createDataPoint(i, lines[i]));
			onScreenPlet[i] = lines[i];

		}

		return res;
	}

	public JSONArray updatePlet(Integer[] lines, int length, int start) {// lines
																			// =
																			// array
																			// met
																			// waarden,
																			// int
																			// length
																			// =
																			// aantal
																			// punten
																			// van
																			// de
																			// grafiek,
																			// start
																			// is
																			// huidige
																			// index
		JSONArray res = new JSONArray();
		// start = start*2;
		if (start < (lines.length - length)) {
			for (int i = 0; i <= start; i++) {
				if (i < onScreenPlet.length) {
					onScreenPlet[i] = lines[length + i];
				} else {
					onScreenPlet[i % onScreenPlet.length] = lines[length + i];
				}
			}
		} else {
			for (int j = 0; j < start; j++) {
				if (j < (lines.length - length) && j < onScreenPlet.length) {
					onScreenPlet[j] = lines[length + j];
				} else {
					onScreenPlet[j % onScreenPlet.length] = lines[length + j];
				}

			}
		}

		for (int i = 0; i < length; i++) {
			res.put(createDataPoint(i, onScreenPlet[i]));
			if (start < i) {
				if (i == start) {
					res.put(createBreakPoint());
				}
			} else {
				if (i == start % length) {
					res.put(createBreakPoint());
				}
			}
		}

		return res;
	}

	public JSONArray CO2Data(Integer[] lines, int length, int start) { // Only
																		// called
																		// first
																		// time
		JSONArray res = new JSONArray();

		for (int i = 0; i < length; ++i) {
			res.put(createDataPoint(i, lines[i]));
			onScreenCO2[i] = lines[i];

		}

		return res;
	}

	public JSONArray updateCO2(Integer[] lines, int length, int start) {// lines
																		// =
																		// array
																		// met
																		// waarden,
																		// int
																		// length
																		// =
																		// aantal
																		// punten
																		// van
																		// de
																		// grafiek,
																		// start
																		// is
																		// huidige
																		// index
		JSONArray res = new JSONArray();
		// start = start*2;
		if (start < (lines.length - length)) {
			for (int i = 0; i <= start; i++) {
				if (i < onScreenCO2.length) {
					onScreenCO2[i] = lines[length + i];
				} else {
					onScreenCO2[i % onScreenCO2.length] = lines[length + i];
				}
			}
		} else {
			for (int j = 0; j < start; j++) {
				if (j < (lines.length - length) && j < onScreenCO2.length) {
					onScreenCO2[j] = lines[length + j];
				} else {
					onScreenCO2[j % onScreenCO2.length] = lines[length + j];
				}

			}
		}

		for (int i = 0; i < length; i++) {
			res.put(createDataPoint(i, onScreenCO2[i]));
			if (start < i) {
				if (i == start) {
					res.put(createBreakPoint());
				}
			} else {
				if (i == start % length) {
					res.put(createBreakPoint());
				}
			}
		}

		return res;
	}

	public JSONArray ECGData(Integer[] lines, int length, int start) { // Only
																		// called
																		// first
																		// time
		JSONArray res = new JSONArray();

		for (int i = 0; i < length; ++i) {
			res.put(createDataPoint(i, lines[i]));
			onScreenECG[i] = lines[i];

		}

		return res;
	}

	public JSONArray updateECG(Integer[] lines, int length, int start) {// lines
																		// =
																		// array
																		// met
																		// waarden,
																		// int
																		// length
																		// =
																		// aantal
																		// punten
																		// van
																		// de
																		// grafiek,
																		// start
																		// is
																		// huidige
																		// index
		JSONArray res = new JSONArray();
		// start = start*2;
		if (start < (lines.length - length)) {
			for (int i = 0; i <= start; i++) {
				if (i < onScreenECG.length) {
					onScreenECG[i] = lines[length + i];
				} else {
					onScreenECG[i % onScreenECG.length] = lines[length + i];
				}
			}
		} else {
			for (int j = 0; j < start; j++) {
				if (j < (lines.length - length) && j < onScreenECG.length) {
					onScreenECG[j] = lines[length + j];
				} else {
					onScreenECG[j % onScreenECG.length] = lines[length + j];
				}

				/*
				 * else if(j < onScreenECG.length){ onScreenECG[j] =
				 * lines[(lines.length-length) + (j%lines.length)]; } else{
				 * onScreenECG[j%onScreenECG.length] =
				 * lines[(lines.length-length) + (j%lines.length)]; }
				 */

			}
		}

		for (int i = 0; i < length; i++) {
			res.put(createDataPoint(i, onScreenECG[i]));
			if (start < i) {
				if (i == start) {
					res.put(createBreakPoint());
				}
			} else {
				if (i == start % length) {
					res.put(createBreakPoint());
				}
			}
		}

		return res;
	}

	private JSONArray createDataPoint(int x, int y) { // returns [x,y]
		JSONArray res = new JSONArray();
		try {
			res.put(x);
			res.put(y);
		} catch (Exception ex) {
			// do something
		}

		return res;
	}

	private JSONArray createBreakPoint() { // returns [x,y]
		JSONArray res = new JSONArray();
		try {
			res.put("null");
		} catch (Exception ex) {
			// do something
		}

		return res;
	}

	private JSONObject getLineOptionsJSON(String lineColor, int min, int max) {
		// TODO Arguments
		JSONObject res = new JSONObject();

		try {
			res.put("series", series(0, lineColor));
			res.put("yaxis", axis(min, max));
			
			
			res.put("grid", grid(true, "#000000"));
		} catch (Exception ex) {
			// do something
		}
		return res;
	}

	private JSONObject grid(boolean show, String backGrCol) {
		JSONObject res = new JSONObject();
		try {
			res.put("show", show);
			res.put("backgroundColor", backGrCol);
			res.put("clickable", true);
		} catch (Exception ex) {
			// do something
		}
		return res;
	}

	private JSONObject series(int shadowSize, String color) { // set shadowSize
																// and lineColor
		JSONObject res = new JSONObject();
		try {
			res.put("shadowSize", shadowSize);
			res.put("color", color);
		} catch (Exception ex) {
			// do something
		}
		return res;
	}

	private JSONObject axis(int minInt, int maxInt) { // create min, max JSON
														// object for an axis
		JSONObject res = new JSONObject();
		try {
			res.put("min", minInt);
			res.put("max", maxInt);
		} catch (Exception ex) {
			// do something
		}

		return res;
	}

	private JSONObject show(boolean bool) { // create show true/false JSON
											// object
		JSONObject res = new JSONObject();
		try {
			res.put("show", bool);
		} catch (Exception ex) {
			// do something
		}

		return res;
	}

	public String getColorofLine() {
		return colorofLine;
	}

	public void setColorofLine(String colorofLine, String colorofLine2,
			String colorofLine3, String colorofLine4) {
		this.colorofLine = colorofLine;
		this.colorofLine2 = colorofLine2;
		this.colorofLine3 = colorofLine3;
		this.colorofLine4 = colorofLine4;
	}

}
